/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CastleDefense;


import java.awt.Image;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


public class Animation
{
    /*
     * Variables
     */
    private ArrayList<Image> frames;
    private int              currentFrame;
    private boolean          done;
    private int              framePeriod;
    private long             frameTicker;
    private long             gameTime;
    
    public Animation(ArrayList<Image> animationFrames, int fps)
    {
        frames = new ArrayList<Image>();

        for(Image frameImage : animationFrames)
            frames.add(frameImage);
        
        gameTime     = System.currentTimeMillis();
        frameTicker  = System.currentTimeMillis();
        framePeriod  = 1000 / fps;
        currentFrame = 0;
        done         = false;
    }

    public void start()
    {
        if (gameTime > frameTicker + framePeriod) 
        {
            frameTicker = gameTime;
            
            currentFrame++;
            if (currentFrame >= frames.size())
                currentFrame = 0;
        }
    }

    public Image getImage()
    {
        gameTime = System.currentTimeMillis();

        return frames.get(currentFrame);
    }

    public boolean isDone()
    {
        return (currentFrame==frames.size()-1);
    }

    public void stop()
    {
        this.done = true;
    }
}